package it.ipermemory.graphic;

import it.ipermemory.R;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Represents a dialog with a loading screen while waiting for the execution of
 * other tasks in the background.
 * 
 * @author Gianluca Carlesso (gianluca.carlesso@gmail.com)
 * @version 1.0
 */
public class ProgressDialogCustom extends Dialog implements OnClickListener {

	/**
	 * Text of message for the dialog
	 */
	private TextView textMessage;

	/**
	 * Object for the preparation of a custom font
	 */
	private Typeface typface;

	/**
	 * Initialize a new object that represents the progress dialog
	 * 
	 * @param context
	 *            of application
	 */
	public ProgressDialogCustom(Context context) {
		super(context, android.R.style.Theme_Translucent_NoTitleBar);

		// Some customize of style
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.custom_progress_dialog);

		// Customize font
		typface = Typeface.createFromAsset(context.getAssets(),
				"fonts/dimbo.ttf");

		textMessage = (TextView) findViewById(R.id.text_message);
		textMessage.setTypeface(typface);

		// I prevent the deletion of the pop-up without selecting an
		setCanceledOnTouchOutside(false);
		setCancelable(false);
	}

	/**
	 * Action to be performed at the click of dialog. In this case there is no
	 * action required
	 * 
	 * @param v
	 *            object of click
	 */
	public void onClick(View v) {

	}

	/**
	 * Set the background of the dialog
	 * 
	 * @param background
	 *            object that defines the background of the dialog
	 */
	@SuppressWarnings("deprecation")
	public void setBackgroundDrawable(Drawable background) {
		LinearLayout linearLayout = (LinearLayout) findViewById(R.id.custom_progress_dialog_layout);

		// backward compatibility to API previous 16
		linearLayout.setBackgroundDrawable(background);
	}
}
